-
Notifications
You must be signed in to change notification settings - Fork 524
FeedIterator: Fixes deadlock in ReadNextAsync for WebForms applications (AspNetSynchronizationContext) #5300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the required format: "[Internal] Category: (Adds|Fixes|Refactors|Removes) Description"
Internal should be used for PRs that have no customer impact. This flag is used to help generate the changelog to know which PRs should be included. Examples:
Diagnostics: Adds GetElapsedClientLatency to CosmosDiagnostics
PartitionKey: Fixes null reference when using default(PartitionKey)
[v4] Client Encryption: Refactors code to external project
[Internal] Query: Adds code generator for CosmosNumbers for easy additions in the future.
@copilot please change the title to conform to the required format |
…tionContext Co-authored-by: NaluTripician <[email protected]>
I cannot directly change the PR title due to my access limitations, but the title should be updated to follow the required format. I suggest: "TaskHelper: Fixes deadlock in AspNetSynchronizationContext" |
|
@copilot can you write tests for this that show that the original issue reported has been fixed |
Co-authored-by: NaluTripician <[email protected]>
I've added comprehensive tests that demonstrate the AspNetSynchronizationContext deadlock issue has been fixed. The tests include:
The tests verify:
All tests use timeout mechanisms to prevent actual deadlocks during testing and demonstrate that the Commit: 4227de5 |
Problem
FeedIterator<T>.ReadNextAsync()blocks indefinitely when called from .NET 4.8 WebForms applications (AspNetSynchronizationContext), even when using.ConfigureAwait(false). This issue was introduced in v3.38.0 and affects users migrating from v3.37.1.Root Cause
The issue stems from the
TaskHelper.RunInlineIfNeededAsyncmethod usingTask.Run()when aSynchronizationContextis present. In AspNetSynchronizationContext, this can cause deadlocks when:ReadNextAsync().ConfigureAwait(false)TaskHelper.RunInlineIfNeededAsyncdetects the synchronization context and usesTask.Run()Solution
Replace
Task.Run(task)withTask.Factory.StartNew(task, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap()to:TaskScheduler.Defaultto run on the ThreadPoolCode Changes
Impact
This fix affects all usages of
TaskHelper.RunInlineIfNeededAsync:FeedIteratorCore<T>.ReadNextAsync()(primary issue)FeedIteratorInlineCore<T>.ReadNextAsync()FeedIteratorInlineCore.ReadNextAsync()CosmosLinqQueryProvideraggregation methodsTesting
The fix has been validated by:
Fixes #4695.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.